using System;

namespace IQIGame.Onigao.Game
{
    public static class DateTimeUtil
    {
        public const int SecondsPerMinute = 60;
        public const int SecondsPerHour = 3600;
        public const int SecondsPerDay = 86400;

        public const long MillisPerSecond = 1000;
        public const long MillisPerMinute = 60000;
        public const long MillisPerHour = 3600000;
        public const long MillisPerDay = 86400000;

        public static DateTime GetDateTime(long timestampMillis, int timeRawOffset)
        {
            return DateTime.UnixEpoch.AddMilliseconds(timestampMillis + timeRawOffset * MillisPerHour);
        }

        /// <summary>
        /// 服务器所在时区本地时间转UTC
        /// </summary>
        /// <param name="serverLocalTime"></param>
        /// <param name="timeRawOffset"></param>
        /// <returns></returns>
        public static DateTime ConvertToUTC(this DateTime serverLocalTime, int timeRawOffset)
        {
            return serverLocalTime.AddMilliseconds(-timeRawOffset * MillisPerHour);
        }

        /// <summary>
        /// 获取时间戳（秒）
        /// </summary>
        /// <param name="serverLocalTime"></param>
        /// <param name="timeRawOffset"></param>
        /// <returns></returns>
        public static double GetTimeStampSec(this DateTime serverLocalTime, int timeRawOffset)
        {
            return serverLocalTime.ConvertToUTC(timeRawOffset).Subtract(DateTime.UnixEpoch).TotalSeconds;
        }

        public static DateTime GetDateTime(long ticks)
        {
            return new DateTime(ticks, DateTimeKind.Utc);
        }

        public static bool TryParseDateStr(string dateTimeFormat, int timeRawOffset, out DateTime outTime)
        {
            if (!DateTime.TryParse(dateTimeFormat, out outTime))
            {
                return false;
            }
            outTime = outTime.ToUniversalTime().AddHours(timeRawOffset);
            return true;
        }
    }
}
